Improve .Distinct().ToList() and .Union(e).ToList()#95224
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-linq Issue Details
public class Bench
{
[Params(10, 1000, 1_000_000)]
public int Count { get; set; }
private int[] _arr;
[GlobalSetup]
public void Init()
{
_arr = new int[Count];
new Random(3).NextBytes(MemoryMarshal.AsBytes(_arr.AsSpan()));
}
[Benchmark]
public List<int> DistinctToList()
{
return _arr.Distinct().ToList();
}
}
|
|
@dotnet-policy-service agree |
|
Can you compare with latest main branch too, and include memory diagnoser? |
Ah, yes, thank you for saying this, it is indeed less of an improvement than that first bench shows, most of the improvements gained here were also gained with #86796 (when I had checked earlier, I had thought this PR was included in net8, which was why I just benched against that, but looking again it was not). Here are the new results:
Still a very small improvement, but I apologize for the misleading benchmark in the original description 😅 |
eiriktsarpalis
left a comment
There was a problem hiding this comment.
Still a very small improvement, but I apologize for the misleading benchmark in the original description 😅
No worries at all. This simplifies the implementation while still doing better than main. Thank you for the contribution :-)
Enumerable.HashSetToListfills the result list by copying from the set to the list itself, but this can be done faster (and more simply) by using theList<T>(IEnumerable<T>)constructor to callHashSet<T>.CopyTo(T[]).